<?php

namespace Boxman\Controller;

use Boxman\Controller\BaseController;

class OrderController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        if (!$this->boxman_id) {
            $this->error('请先登录');
        }
    }
    
    public function boxmanOrderList(){
        $boxman_id = $this->boxman_id;
        //这里移动端让这么传，不用
//        $boxman_order_status = $this->getParam('boxman_order_status', 10, 'int+');
//        $where['oa.boxman_order_status'] = $boxman_order_status;
        $order_status = $this->getParam('order_status', -1, 'int+');
        $page = $this->getParam('page', 1, 'int+');
        
        if ($order_status != -1) {
            $where['o.order_status'] = $order_status;
        }else{
            $where['o.order_status'] = array('in',[20,30,31]);
        }
        
        $where['sa.boxman_id'] = $boxman_id;
        $list = M()->table('ot_shipment_address sa')
                    ->field('o.*,p.port_name,d.dock_name,d.dock_price,dr.driver_id,dr.driver_phone,dr.driver_name,dr.driver_phone,y.yard_name,y.yard_price,la.loadarea_name')
                    ->join('ot_order_address AS oa ON oa.address_id = sa.shipment_address_id AND address_type=2')
                    ->join('ot_order as o ON o.order_id = oa.order_id')
                    ->join('LEFT JOIN ot_dock as d ON d.dock_id = o.dock_id')
                    ->join('LEFT JOIN ot_port as p ON p.port_id = o.port_id')
                    ->join('LEFT JOIN ot_driver as dr ON dr.driver_id = o.driver_id')
                    ->join('LEFT JOIN ot_yard as y ON y.yard_id = o.yard_id')
                    ->join('LEFT JOIN ot_load_area as la ON la.loadarea_id = o.loadarea_id')
                    ->where($where)
                    ->order('o.order_id DESC')
                    ->page($page)
                    ->limit(10)
                    ->select();
        
        $this->returnList($list);
    }
    
    //装箱工订单统计数
    public function orderCountList(){
        $boxman_id = $this->boxman_id;
//        $Order = M('Order');
//        $order_status = $this->getParam('order_status', 0, 'int+');
//        $where['o.order_status'] = $order_status;
//        $where['sa.boxman_id'] = $boxman_id;
//        $where['sa.boxman_order_status'] = 10;
//        $return['wait_car'] = M()->table('ot_shipment_address sa')
//                    ->join('ot_order_address AS oa ON oa.address_id = sa.shipment_address_id AND address_type=2')
//                    ->join('ot_order as o ON o.order_id = oa.order_id')
//                    ->where($where)
//                    ->count();
        $return['allotted'] = M()->table('ot_shipment_address sa')
                            ->join('ot_order_address AS oa ON oa.address_id = sa.shipment_address_id AND address_type=2')
                            ->join('ot_order as o ON o.order_id = oa.order_id')
                            ->where(array('o.order_status'=>20,'sa.boxman_id'=>$boxman_id))
                            ->count();
        $return['under_way'] = M()->table('ot_shipment_address sa')
                                ->join('ot_order_address AS oa ON oa.address_id = sa.shipment_address_id AND address_type=2')
                                ->join('ot_order as o ON o.order_id = oa.order_id')
                                ->where(array('o.order_status'=>30,'sa.boxman_id'=>$boxman_id))
                                ->count();
        
        $this->returnList($return);
    }
    
    public function boxmanOrderDetail(){
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE);

        $where['o.order_id'] = $order_id;
        $orderInfo = M()->table('ot_order o')->field('o.*,p.port_name,d.dock_name,d.dock_price,dw.weight_desc,dr.driver_id,dr.driver_name,dr.driver_phone,dr.car_no,dr.driver_lon,driver_lat')
                ->join('LEFT JOIN ot_dock as d ON d.dock_id = o.dock_id')
                ->join('LEFT JOIN ot_port as p ON p.port_id = d.port_id')
                ->join('LEFT JOIN ot_dock_weight as dw ON dw.port_id = d.port_id')
                ->join('LEFT JOIN ot_driver as dr ON dr.driver_id = o.driver_id')
                ->where($where)
                ->find();
        if ($orderInfo) {
            $OrderAddress = M('order_address');
            $orderAddressList = $OrderAddress->where(array('order_id' => $orderInfo['order_id']))->order('order_address_id ASC')->select();
            if ($orderAddressList) {
                $orderInfo['box_address'] = array();
                $orderInfo['shipment_address'] = array();
                foreach ($orderAddressList as $val) {
                    if ($val['address_type'] == 1) {
                        $orderInfo['box_address'][] = json_decode($val['json_content'], TRUE);
                    }
                    
                    if ($val['address_type'] == 2) {
                        $orderInfo['shipment_address'][] = json_decode($val['json_content'], TRUE);
                    }
                }
            }
            //物流信息
            $orderInfo['logistics'] = M('order_logistics')->where(['order_id' => $order_id])->order('order_logistics_id DESC')->select();
            
            if(!empty($orderInfo['box_img'])){
                $arr_box_img = json_decode($orderInfo['box_img'], TRUE);
                foreach($arr_box_img as &$vv){
                    $vv = C('SITE_URL').$vv;
                }
                unset($vv);
            }
            $orderInfo['box_img'] = $arr_box_img;
        }
        $this->returnList($orderInfo);
    }
    
    //装箱工完成装箱
    public function completeBox(){
        $boxman_id = $this->boxman_id;
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE);
        $res = M('order_address')->where(array('order_id'=>$order_id,'linkman_id'=>$boxman_id))->save(['boxman_order_status'=>20]);
        $this->success();
    }
}
